package com.fingard.dsp.bank.directbank.citic04.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class yldfdemo {
    /**
     * @param unicode unicode
     * @return 结果字符串
     * @Description:济南银联unicode转字符串
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();

        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; ++i) {
            int data = Integer.parseInt(hex[i], 16);

            string.append((char) data);
        }

        return string.toString();
    }

    /**
     * @param string 待转换串
     * @return 结果串
     * @Description:济南银联字符串转unicode
     */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); ++i) {
            char c = string.charAt(i);

            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }

    /**
     * @param str 待压缩内容
     * @return 压缩结果
     * @Description:济南银联zip压缩
     */
    public static final String zip(String str) {
        str = string2Unicode(str);
        if (str == null) {
            return null;
        }
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        byte[] compressed;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            byte[] strbyte = str.getBytes();
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = new BASE64Encoder().encodeBuffer(compressed);
        } catch (IOException e) {
            compressed = (byte[]) null;
        } finally {
            if (zout != null)
                try {
                    zout.close();
                } catch (IOException localIOException3) {
                }
            if (out != null)
                try {
                    out.close();
                } catch (IOException localIOException4) {
                }
        }
        return compressedStr;
    }

    /**
     * @param compressedStr 待解压内容
     * @return 解压结果
     * @Description:济南银联解压
     */
    public static final String unzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {
            byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null)
                try {
                    zin.close();
                } catch (IOException localIOException4) {
                }
            if (in != null)
                try {
                    in.close();
                } catch (IOException localIOException5) {
                }
            if (out != null)
                try {
                    out.close();
                } catch (IOException localIOException6) {
                }
        }
        return unicode2String(decompressed);
    }
}